//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES OR INDEMNITIES.
//
/*++
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.
Copyright (c) 1995-2003  Microsoft Corporation

Module Name: hw_related.cpp

Abstract:


Notes:


--*/
//
//
// (C) Copyright 2007 Marvell International Ltd.
//  All Rights Reserved
//

#include <windows.h>

#ifdef SAGEM_MO190

#include "xsc1.h"
#include "gpio.h"
#include "drvlib.h"

// define GPIO registers
// out signals
#define MOD_OFF_CMD             GPIO_80    // active high
#define MOD_ON_CMD              GPIO_73    // active high
#define MOD_RESET_CMD           GPIO_70    // active high
#define DTE_UART_STATE          GPIO_87    // active high

// in signals
#define MOD_ON_STATE            GPIO_45    // active high
#define MOD_RESET_STATE         GPIO_57    // active high

// gpio registers
#define MOD_OFF_CMD_GPDR        GPDR_z
#define MOD_OFF_CMD_GPCR        GPCR_z
#define MOD_OFF_CMD_GPSR        GPSR_z

#define MOD_ON_CMD_GPDR         GPDR_z
#define MOD_ON_CMD_GPCR         GPCR_z
#define MOD_ON_CMD_GPSR         GPSR_z

#define MOD_RESET_CMD_GPDR      GPDR_z
#define MOD_RESET_CMD_GPCR      GPCR_z
#define MOD_RESET_CMD_GPSR      GPSR_z

#define DTE_UART_STATE_GPDR     GPDR_z
#define DTE_UART_STATE_GPCR     GPCR_z
#define DTE_UART_STATE_GPSR     GPSR_z

#define MOD_ON_STATE_GPDR       GPDR_y
#define MOD_ON_STATE_GPLR       GPLR_y

#define MOD_RESET_STATE_GPDR    GPDR_y
#define MOD_RESET_STATE_GPLR    GPLR_y

// power management unit
#include "oemioctl.h"
#include "pmu.h"
#ifdef SENDLOG
    // SENDLOG is also define in pmu.h :-(
    #undef SENDLOG
#endif

#endif  //SAGEM_MO190

#include "dispatcher.h"
#include "chanel.h"
#include <pegdser.h>




// notification that is sent by modem upon boot
#define MODEM_INIT_NOTIF            "\r\n+KSUP: "
#define TIME_OUT_ON_INIT_NOTIF      (120000)            // 2 min

#define TIME_OUT_CPOF               (5000)              // 5 sec

#include "rhaapi.h"
#define WAIT_FOR_START_TIMEOUT            30000
extern RHA_FUNCTION_LIST g_MUX_RHAFunctionList;

//---------------------------------------------------------------
// Radio On or Off 
//---------------------------------------------------------------
bool CDispatcher::SwitchRadioOff() 
{
    // switch radio off and close COM port
    return SwitchRadioOff(true) ;
}

bool CDispatcher::SwitchRadioOff(bool bCloseCOMPort)
{
    g_MUX_RHAFunctionList.pfnRHA_RadioPwrOff();
    return true ;
}

bool CDispatcher::SwitchRadioOn()
{
    bool fRet = false ;

    if (!m_bIsOk)
    {
        if (!g_MUX_RHAFunctionList.pfnRHA_RadioPwrOn())
        {
            SENDERR((TEXT("[710MUX]: Dispatcher: -> SwitchRadioOn radio failed\r\n")));        
            return FALSE;
        }
        
        DWORD dwTimeout, dwStartTime, dwMask;
    
        dwTimeout = WAIT_FOR_START_TIMEOUT;


        // get the initial tick count
        dwStartTime = GetTickCount();

        // Initial delay to give signals some time to settle down
        Sleep(500);

        // Now wait for the CTS signal to become signaled
        while (TRUE)
        {
            if (!GetCommModemStatus(m_hComPort, &dwMask))
            {
                SENDERR((TEXT("[710MUX]:: Error - Unable to get comm modem status\r\n")));
                return FALSE;
            }
        
            if (dwMask & MS_CTS_ON) {
              // Active CTS signal detected -> module is on now
              break;
            }

            if ((GetTickCount() - dwStartTime) >= dwTimeout)
            {
                // We timed out
                SENDERR((TEXT("[710MUX]:: Error - WaitForModuleStart() timed out!\r\n")));
                return FALSE;
            }
        }
        
        // Final delay to give module last chance to settle things
        Sleep(200);
  
        SENDERR((TEXT("[710MUX]:: Module started after %d ms\r\n"), GetTickCount() - dwStartTime));
  
        fRet = TRUE;
        
    }
    else
    {
        SENDERR((TEXT("710MUX: Dispatcher: -> SwitchRadioOn radio already on\r\n"))) ;
        fRet = true ;
    }

    goto Exit;

Exit:
    return fRet ;
}

bool CDispatcher::ResetRadio()
{
    return SwitchRadioOn() ;
}

//--------------------------------------------------------------------
// Activity monitoring
//--------------------------------------------------------------------
void CUserChanel::NotifyActivity(tActivityStatus ActivityStatus)
{
#if !defined(GSM0710_UNITARY_TESTS) && defined(SAGEM_MO190)
    DWORD dwStatus; 
    DWORD dwBytesNb;
    DWORD dwPmuStatusReport ;
    HANDLE hPMU ;
    bool bReport = false ;
#endif

    switch (ActivityStatus)
    {
        // Open Channel
        // Close Channel
        //---------------
        case CHANNEL_OPEN:
        case CHANNEL_CLOSE:
            // do nothing
            break ;

        // No activity Channel
        //----------------------
        case CHANNEL_IDLE:
            SENDLOG((TEXT("710MUX: CChanel %s - NotifyActivity CHANNEL_IDLE\r\n"), m_pName)) ;

#if !defined(GSM0710_UNITARY_TESTS) && defined(SAGEM_MO190)
            if (m_bATChnl)
            {
                dwPmuStatusReport = VCOM_AT_CH_IDLE ;
            }
            else
            {
                dwPmuStatusReport = VCOM_DATA_CH_IDLE ;
            }
            bReport = true ;
#endif
            break ;

        // Activity Channel
        //----------------------
        case CHANNEL_BUSY:
            SENDLOG((TEXT("710MUX: CChanel %s - NotifyActivity CHANNEL_BUSY\r\n"), m_pName)) ;

#if !defined(GSM0710_UNITARY_TESTS) && defined(SAGEM_MO190)
            if (m_bATChnl)
            {
                dwPmuStatusReport = VCOM_AT_CH_BUSY ;
            }
            else
            {
                dwPmuStatusReport = VCOM_DATA_CH_BUSY ;
            }
            bReport = true ;
#endif
            break ;
    }

#if !defined(GSM0710_UNITARY_TESTS) && defined(SAGEM_MO190)
    if (bReport)
    {
        // check if the device is fully powered
        hPMU = CreateFile(    TEXT("PMU1:"), 
                            GENERIC_READ,
                            FILE_SHARE_READ|FILE_SHARE_WRITE, 
                            0, 
                            OPEN_EXISTING, 
                            FILE_ATTRIBUTE_NORMAL|FILE_FLAG_RANDOM_ACCESS, 
                            0 ) ;

        if ( INVALID_HANDLE_VALUE == hPMU )
        {
            SENDLOG((TEXT("710MUX: CChanel %s - NotifyActivity cannot open PMU driver\r\n"), m_pName)) ;
            return ;
        }

        DeviceIoControl(hPMU, IOCTL_PMU_VCOM_STATUS, &dwPmuStatusReport, sizeof(DWORD), 
                        &dwStatus, sizeof(DWORD), &dwBytesNb, NULL) ; 

        CloseHandle(hPMU) ;
    }
#endif
}




